package com.happy.xiao.publisher.controller;


import com.happy.xiao.common.base.BaseResponseInfo;
import com.happy.xiao.common.dto.UserDTO;
import com.happy.xiao.common.rabbitmq.service.IDLXMessage;
import com.happy.xiao.common.rabbitmq.service.INormalMessage;
import com.happy.xiao.common.redis.IRedisService;
import com.happy.xiao.common.utils.StringUtil;
import com.happy.xiao.common.utils.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rabbitSend")
public class RabbitSendController {

    private Logger logger = LoggerFactory.getLogger(RabbitSendController.class);


    @Autowired
    private Environment env;

    @Autowired
    IRedisService iRedisService;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @Autowired
    IDLXMessage idlxMessage;

    @Autowired
    INormalMessage iNormalMessage;





    /**
     * 发送SimpleListener消息
     * <p>
     * 手动确认机制
     * 并发配置
     *
     * @return
     */
    @RequestMapping(value = "/simple", method = RequestMethod.GET)
    public BaseResponseInfo simple() {

        BaseResponseInfo<String> responseInfo = new BaseResponseInfo<>();
        responseInfo.setData("okla");

        try {

            String exchange = env.getProperty("simple.mq.exchange.name");
            String routingKey = env.getProperty("simple.mq.routing.key.name");


            UserDTO userDTO = new UserDTO(1, "zhangsan" + StringUtil.time(), "helloworld");
            String dtoJson = Util.ObjectToJson(userDTO);

//            iNormalMessage.sendSimpleMessage(dtoJson);
            System.out.println("send -----------------------------------------------");
            iNormalMessage.sendSimpleMessage(dtoJson, "www.jd.com");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseInfo;
    }


    /**
     * 发送SimpleListener消息
     * <p>
     * 手动确认机制
     * 并发配置
     *
     * @return
     */
    @RequestMapping(value = "/delayMessage", method = RequestMethod.GET)
    public BaseResponseInfo delayMessage() {

        BaseResponseInfo<String> responseInfo = new BaseResponseInfo<>();
        responseInfo.setData("okla");

        try {
            UserDTO userDTO = new UserDTO(1, "Delay-order2222" + StringUtil.time(), "hello");
            idlxMessage.sendCommonDelayMessage(userDTO, "www.baidu.com", 5000, 30000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return responseInfo;
    }


    /**
     * 发送SimpleListener消息
     * <p>
     * 手动确认机制
     * 并发配置
     *
     * @return
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public BaseResponseInfo test() {

        BaseResponseInfo<String> responseInfo = new BaseResponseInfo<>();
        responseInfo.setData("okla");

        try {

            iRedisService.increment("money", -1, 100);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return responseInfo;
    }

}
